//
//  RequestURL.m
//  LO_MUSIC
//
//  Created by 向祖华 on 16/2/25.
//  Copyright © 2016年 杨少锋. All rights reserved.
//

//#define kHomeUrl @"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist"


#import "RequestURL.h"

//定义一个存储网址的常量
static NSString* const kHomeUrl = @"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist";

@implementation RequestURL

//音乐首页的网络请求方法实现
-(void)requestMusiclistWithSuccess:(successBlock)success faile:(faileBlock)faile
{
    //放到子线程中
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        //请求数据
        NSArray * arr = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:kHomeUrl]];
        
        //如果请求到的数据为空
        if (arr.count == 0)
        {
            NSError * err = [NSError errorWithDomain:@"请求出错" code:0 userInfo:nil];
            //回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                faile(err);
                return ;
            });
        }
        
        //创建一个数组 盛放模型对象
        NSMutableArray * modelArray = [NSMutableArray array];
        //解析
        for (NSDictionary* dic in arr) {
            MusicMode * model = [[MusicMode alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [modelArray addObject:model];
        }
        
        //判断block是否存在
        if (success) {
            //回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                success(modelArray);
            }); 
        }
    });
    
    
}

@end

@implementation MusicMode
//写出model免崩溃的方法
-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}

@end